/**
 * Get a new XMLHttpRequest
 */
function createRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				xmlreq = false;
			}
		}
	}
	return xmlreq;
}

/**
 * set request to the handler
 */
function getReadyStateHandler(req, responseHandler, isXML) {
	return function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				if (isXML==true)
					responseHandler(req.responseXML);
				else
					responseHandler(req.responseText);
			} else {
				alert("HTTP error " + req.status + ": " + req.statusText);
			}
		}
	};
}

/**
 * send a POST request in XML with arguments
 * callback handler if it is not null
 * @param url
 * @param arguments
 * @param handler
 */
function sendXMLPost(url, arguments, handler) {
	var req = createRequest();		
	req.open("POST", url, true);
	req.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
	if (arguments!=null) {
		//req.setRequestHeader("Content-Length", arguments.length);
	}
	if (handler != null)
		req.onreadystatechange = getReadyStateHandler(req, handler, true);
	req.send(arguments);
}

/**
 * add a Tag '<name>value</name>' to the end of XMLString 
 * @param XMLString
 * @param name
 * @param value
 * @returns
 */
function addTag(XMLString, name, value) {
	XMLString += "<" + name + ">" + value + "</" + name + ">";
	return XMLString; 
}

/**
 * catch event and deal it with handler
 */ 
function catchEvent(eventObj, event, eventHandler) {
	  if (eventObj.addEventListener) {
		  eventObj.addEventListener(event, eventHandler, false);
	  } else if (eventObj.attachEvent) {
		  event = "on" + event;
		  eventObj.attachEvent(event, eventHandler);
	  }
}


function cancelEvent(event) {
	  if (event.preventDefault) {
		  event.preventDefault();
		  event.stopPropagation();
	  } else {
		  event.returnValue = false;
		  event.cancelBubble = true;
	  }
}

/*
 * setup events use catchEvent
 */
function setupEvents(evnt) {
	//catchEvent(document.getElementById("orderform"), "submit", login);
}

//setup events onload
//catchEvent(window, "load", setupEvents);

/**
 * jump to url
 */
function jumpTo(url) {
	window.location=url;
}

/**
 * get username from cookie
 * @returns username
 */
function getUserName() {
	var cookies = document.cookie.split("; ");
	for ( var i = 0; i < cookies.length; i++) {
		if (cookies[i].split("=")[0] == "username") {
			var username = cookies[i].split("=")[1];
			return username;
		}
	}
}